import requests
import time
from re import findall
from PIL import Image
# import pytesseract
#获取验证码
def get_code(s,headers,code_url):
    
    r = s.get(url = code_url,headers = headers)
    with open('code.png','wb') as f:
        f.write(r.content)
        f.close
    img=Image.open('code.png')
    img.show()
    # num = pytesseract.image_to_string(img)
    captcha = input('请查看验证码并输入：')
    # print('\n')
    return captcha#返回cookies作为整个会话用

#登陆
def login(s,login_url,headers,captcha,login_data):
    
    login_data['CheckCode'] = str(captcha)
    r = s.post(url = login_url,data = login_data,headers =headers)

def get_page(s,headers,pj_page_url):
    r = s.get(url = pj_page_url,headers = headers)
    # if r.status_code == 200:
    #     print('登陆并获取评教总页面成功')
    #     print('\n')
    # else:
    #     print('登陆失败，请检查验证码及密码')
    #     print('\n')
    # print(r.text)
    return r
# 解析评教信息页面   
def info_patten(html):
                         
    info_patten = r'<td style="height:25px;">(.*?)</td><td>(.*?)</td><td>(.*?)</td><td>(.*?)</td><td>(.*?)</td><td>(.*?)</td><td>(.*?)</td><td>(.*?)</td><td>(.*?)</td><td>(.*?)</td>'
    result = findall(info_patten,html)
    return result
# 解析没评教的课堂的评教链接
def url_patten(html):
    url_patten = r"'Stu_Assess_(.*?)','学生评教"
    result = findall(url_patten,html)
    return result
# 解析最终的评教完成页面
def last_state_patten(html):
    last_state_patten = r"<script language='javascript' defer>alert(.*?);</script></form>"
    result = findall(last_state_patten,html)
    # print(result)
    if result == ["('你的评教结果已被成功保存！请关闭本窗口！')"]:
        last_state = 1
    else:
        last_state = 0
    return last_state
# 解析post需要附带的页面信息
def view_patten(html):
    view_patten1 = r'<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="(.*?)" />'
    view_patten2 = r'<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="(.*?)" />'
    result1 = findall(view_patten1,html)
    result2 = findall(view_patten2,html)
    return result1,result2
# 获取评教信息页面
def view_page(s,headers,one_page_url):
    r = s.get(url = one_page_url, headers = headers)
    # print(r.text)
    return r.text
# 获取单个评教链接的页面
def one_page(s,headers,one_page_url,data):
    # print(data)
    r = s.post(url = one_page_url, headers = headers,data = data)
    return r.text

def main():
    print('\n'+'***Write By 盛意以山河***'+'\n')
    print('欢迎使用自动评教脚本，默认全部老师好评，请悉知。','\n')
    s = requests.Session()
    code_url = 'http://210.42.38.26:84/jwc_glxt/ValidateCode.aspx'#验证码地址
    login_url = 'http://210.42.38.26:84/jwc_glxt/Login.aspx'#登陆地址
    pj_page_url = 'http://210.42.38.26:84/jwc_glxt/Stu_Assess/Stu_Assess.aspx'#评教总页面
    headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.7 Safari/537.36'
    }
    login_data = {
        '__VIEWSTATE': '/wEPDwUKLTQ4NjU1OTA5NGQYAQUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgEFCGJ0bkxvZ2lu87YBxXeCmB2j7CocJsNhyfsKzWE=',
        '__EVENTVALIDATION': '/wEWBQLYts3EAgKl1bKzCQKC3IeGDAK1qbSRCwLO44u1Dfpw4XbGYape7ys17s2u91txfgZM',
        'txtUserName': '',
        'btnLogin.x': '52',
        'btnLogin.y': '17',
        'txtPassword': '',
        'CheckCode': ''
    }
    login_data['txtUserName'] = input('请输入学号：')
    login_data['txtPassword'] = input('请输入密码：')
    captcha = get_code(s,headers,code_url)    
    login(s,login_url,headers,captcha,login_data)
    pj_html = get_page(s,headers,pj_page_url)

    sum = len(info_patten(pj_html.text))
    if sum == 0:
        print('登陆失败，请检查密码或验证码后重试!\n')
    else:
        print('登陆成功！\n')
    finsh = 0
    finshed = 0
    print('*******总共有'+str(sum)+'门课*******\n')
    for i in range(len(info_patten(pj_html.text))):
        state1 = ' '.join(info_patten(pj_html.text)[i])
        # print(state1)
        state2 = url_patten(state1)
        # print(state2)
        # print(state1)

        if state2 == []:#评过了
            print('【'+info_patten(pj_html.text)[i][3]+', '+info_patten(pj_html.text)[i][5]+', '+info_patten(pj_html.text)[i][6]+'】'+'    该门课原本已完成评教')
            finshed += 1
            print('\n')
        elif len(state2) == 1:#普通课程
            print('【'+info_patten(pj_html.text)[i][3]+', '+info_patten(pj_html.text)[i][5]+', '+info_patten(pj_html.text)[i][6]+'】'+'    该门课可完成一次评教，正在评教中...')
            url = 'http://210.42.38.26:84/jwc_glxt/Stu_Assess/Stu_Assess_'+' '.join(state2)
            # print(url)
            # print(url)
            html = view_page(s,headers,url)
            __VIEWSTATE,__EVENTVALIDATION = view_patten(html)
            # print(__VIEWSTATE,__EVENTVALIDATION)
            if info_patten(pj_html.text)[i][6] == '理论类':
                data = {
                '__VIEWSTATE': '',
                '__EVENTVALIDATION': '',
                'GridCourse2$ctl02$userscore': '18',
                'GridCourse2$ctl03$userscore': '6',
                'GridCourse2$ctl04$userscore': '10',
                'GridCourse2$ctl05$userscore': '10',
                'GridCourse2$ctl06$userscore': '10',
                'GridCourse2$ctl07$userscore': '6',
                'GridCourse2$ctl08$userscore': '10',
                'GridCourse2$ctl09$userscore': '10',
                'GridCourse2$ctl10$userscore': '10',
                'GridCourse2$ctl11$userscore': '10',
                'SuitTeach': 'RadioButton1',
                'TeacherGood': '',
                'TeacherChange': '',
                'TeachForm': 'RadioButton4',
                'HopeTeachForm': 'RadioButton12',
                'TeachTool': 'RadioButton8',
                'btnSave': '·确定·',
                }      
                # print(str(__VIEWSTATE[0]),str(__EVENTVALIDATION[0]))
            if info_patten(pj_html.text)[i][6] == '实践类':
                data = {
                    '__VIEWSTATE': '',
                    '__EVENTVALIDATION': '',
                    'TextBox1': '90',
                    'SuitTeach': 'RadioButton1',
                    'TeacherGood': '' ,
                    'TeacherChange': '',
                    'TeachForm': 'RadioButton4',
                    'HopeTeachForm': 'RadioButton12',
                    'TeachTool': 'RadioButton8',
                    'btnSave': '·确定·',
                }
            
            data['__VIEWSTATE'] = str(__VIEWSTATE[0])
            data['__EVENTVALIDATION'] = str(__EVENTVALIDATION[0])
            html = one_page(s,headers,url,data)
            # print(html)
            last_state = last_state_patten(html)
            if last_state == 1:
                print('评教成功！')
                finsh += 1 
            else:
                print('评教失败，请检查')
            print('\n')
        else:#线上课程，可评评教多个
            print('【'+info_patten(pj_html.text)[i][3]+', '+info_patten(pj_html.text)[i][5]+', '+info_patten(pj_html.text)[i][6]+'】'+'    该门课为线上课程，可完成多次评教，正在评教中...')
            # print(state2)
            # print(type(state2))
            # print(state2[0])
            # print(type(state2[0]))
            for x in range(len(state2)):
                url = 'http://210.42.38.26:84/jwc_glxt/Stu_Assess/Stu_Assess_'+state2[x]
                # print(url)
                html = view_page(s,headers,url)
                __VIEWSTATE,__EVENTVALIDATION = view_patten(html)
                data = {
                '__VIEWSTATE': '',
                '__EVENTVALIDATION': '',
                'GridCourse2$ctl02$userscore': '18',
                'GridCourse2$ctl03$userscore': '6',
                'GridCourse2$ctl04$userscore': '10',
                'GridCourse2$ctl05$userscore': '10',
                'GridCourse2$ctl06$userscore': '10',
                'GridCourse2$ctl07$userscore': '6',
                'GridCourse2$ctl08$userscore': '10',
                'GridCourse2$ctl09$userscore': '10',
                'GridCourse2$ctl10$userscore': '10',
                'GridCourse2$ctl11$userscore': '10',
                'SuitTeach': 'RadioButton1',
                'TeacherGood': '',
                'TeacherChange': '',
                'TeachForm': 'RadioButton4',
                'HopeTeachForm': 'RadioButton12',
                'TeachTool': 'RadioButton8',
                'btnSave': '·确定·',
                }   
                data['__VIEWSTATE'] = str(__VIEWSTATE[0])
                data['__EVENTVALIDATION'] = str(__EVENTVALIDATION[0])
                html = one_page(s,headers,url,data)
                # print(html)
                last_state = last_state_patten(html)
                if last_state == 1:
                    print('第'+str(x+1)+'次评教成功！')
                    finsh += 1
                else:
                    print('评教失败，请检查')
            print('\n')   
    print('***总共有'+str(sum)+'门课待评教，原本已评教'+str(finshed)+'门课，本次评教'+str(finsh)+'门课，请查收!***\n')
    print('\n***该软件仅用作交流学习，请勿非法使用。更多好用软件，请关注作者微信公众号：620黑科技，CTGU更多福利等你发现***\n')
    print('该软件于60秒后自动退出')

if __name__ == "__main__":
    main()
    time.sleep(60)